﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DispatchingService.CustomJob
{
    [PersistJobDataAfterExecution] ///执行后的保留作业数据
    [DisallowConcurrentExecution] //不允许当前执行
    public class SendMessageJob : IJob //(无状态)
    {
        public SendMessageJob()
        {
            Console.WriteLine("SendMessageJob被构造了");
        }

        /// <summary>
        /// 通过静态对象也可以实现链式传参
        /// </summary>
        private static object tempObject = new object();

        /// <summary>
        /// 当前Task内部就作业休要执行的任务
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context) //context是上下文 ，有了context以后我们就可以获取很多信息
        {

            //再实际的任务执行中，可能会比较耗时！

            //在发消息上课的时候，需要指定名称
            await Task.Run(() =>
            {

                Thread.Sleep(5000); //这个方法比较耗时

                Console.WriteLine();
                Console.WriteLine("**************************************");

                var student1 = context.JobDetail.JobDataMap.GetString("student1");
                var student2 = context.JobDetail.JobDataMap.Get("student2");
                var student3 = context.JobDetail.JobDataMap.Get("student3");
                var Year = context.JobDetail.JobDataMap.GetInt("Year");

                Console.WriteLine($"{student1}你好！现在开始上课了！{DateTime.Now}");
                Console.WriteLine($"{student2}你好！现在开始上课了！{DateTime.Now}");
                Console.WriteLine($"{student3}你好！现在开始上课了！{DateTime.Now}");
                Console.WriteLine($"当前年份是：{Year}");

                var student4 = context.Trigger.JobDataMap.Get("student4");
                var student5 = context.Trigger.JobDataMap.GetString("student5");
                var student6 = context.Trigger.JobDataMap.Get("student6");
                var Year1 = context.Trigger.JobDataMap.GetInt("Year");


                Console.WriteLine($"{student4}你好！现在开始上课了！{DateTime.Now}");
                Console.WriteLine($"{student5}你好！现在开始上课了！{DateTime.Now}");
                Console.WriteLine($"{student6}你好！现在开始上课了！{DateTime.Now}");
                Console.WriteLine($"当前年份加了一个1是：{Year1}");

                var Year2 = context.MergedJobDataMap.Get("Year"); //会去重  //以后者为准

                Console.WriteLine($"Year2:{Year2}");

                context.JobDetail.JobDataMap.Put("Year", Year + 1);
                 

                Console.WriteLine("**************************************");
                Console.WriteLine();
            });
        }
    }
}
